package demo1;

import java.io.*;
import java.net.*;

public class MyServer {
    public static void main(String[] args) {
        try{
            ServerSocket server = new ServerSocket(1680);
            Socket socket = server.accept();

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("服务器已启动，等待客户端消息...");
            String s;
            while(( s = in.readLine()) != null){
                System.out.println("收到客户端消息:" + s);
                if("bye".equalsIgnoreCase(s)){
                    System.out.println("客户端请求关闭连接");
                    break;
                }

                System.out.print("请输入回复给客户端的消息：");
                String response = sin.readLine();
                out.println(response);

                if ("bye".equalsIgnoreCase(response)) {
                    System.out.println("服务器主动关闭连接...");
                    break;
                }
            }

            System.out.println("连接正在关闭....");
            in.close();
            out.close();
            socket.close();
            server.close();
        } catch (Exception e) {
            throw new RuntimeException("服务器异常", e);
        }
    }
}


//package demo1;
//
//import java.io.*;
//import java.net.*;
//
//public class MyServer {
//    public static void main(String[] args) {
//        try (ServerSocket server = new ServerSocket(1680);
//             Socket socket = server.accept();
//             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
//             BufferedReader sin = new BufferedReader(new InputStreamReader(System.in))) {
//
//            System.out.println("服务器已启动，等待客户端消息...");
//
//            String clientMessage;
//            // 主要修改：先读取客户端消息，再回复
//            while (true) {
//                // 1. 先读取客户端消息
//                clientMessage = in.readLine();
//                if (clientMessage == null || "bye".equalsIgnoreCase(clientMessage)) {
//                    System.out.println("客户端断开连接或请求关闭");
//                    break;
//                }
//                System.out.println("# 收到客户端消息: " + clientMessage);
//
//                // 2. 然后获取服务器回复
//                System.out.print("请输入回复给客户端的消息: ");
//                String response = sin.readLine();
//                if (response == null || "bye".equalsIgnoreCase(response)) {
//                    System.out.println("服务器主动关闭连接...");
//                    out.println("bye");
//                    break;
//                }
//                out.println(response);
//            }
//
//            System.out.println("连接正在关闭....");
//        } catch (Exception e) {
//            throw new RuntimeException("服务器异常", e);
//        }
//    }
//}